<?php

namespace app\api\model\service;

use think\Model;
use think\Db;
class TotalData extends Model
{
    // 表名
    protected $name = 'service_total_data';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function totalData($type=0)
    {
        $data['total_order_nums'] = Order::where(['status'=>['>',5]])->count();
        $data['total_user_nums'] = User::count();
        $data['total_plus_nums'] = UserInfo::where(['is_plus'=>1])->count();
        $data['total_skill_nums'] = Skill::count();
        $data['total_shop_nums'] = Shop::count();
        $data['total_price'] = Order::where(['status'=>['>',5]])->sum('payprice-refund_price');
        $rebatePrice = Rebate::where(['rebatetype'=>0])->sum('num');
        $carPrice = 0;
        $carList = Rebate::where(['rebatetype'=>2])->group('order_id')->column('id');
        foreach ($carList as $val)
        {
            $carPrice+=Rebate::where('id',$val)->value('num');
        }
        $data['rebate_price'] = bcadd($rebatePrice,$carPrice,2);
        $data['recharge_price'] = Recharge::where(['state'=>1])->sum('price');
        $data['withdraw_price'] = Withdraw::where(['state'=>['>',1],'type'=>['<>',1]])->sum('num');
        $data['skill_ensure_price'] = Skill::sum('ensure_price');
        $data['shop_ensure_price'] = Shop::sum('ensure_price');
        $starttime = strtotime(date("Y-m-d",time()));
        $data['day_order_nums'] = $type == 0?Order::where(['status'=>['>',5],'finishtime'=>['>',$starttime]])->count():Order::where(['status'=>['>',5],'finishtime'=>['between',[$starttime-86400,$starttime]]])->count();
        if($type == 1)
        {
            self::create($data);
        }
        return $data;
    }
}
